﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using YRevitJig;

namespace YJKRevitDimension.PlaneSlopeDimension
{
	public class LeaderDimJig : XUiJigBase
	{
		public XYZ StartPoint { get; set; }

		public double Angle { get; set; }

		public double Distance { get; set; }

		public bool IsUseCustomAngle { get; set; }

		public LeaderDimJig(UIDocument uiDoc) : base(uiDoc)
		{
			this.Angle = 45.0;
			this.Distance = 100.0;
			this.IsUseCustomAngle = false;
			this.UpdateData();
		}

		public override List<Curve> CreateCurves()
		{
			return new List<Curve>();
		}

		protected void UpdateData()
		{
			this.Angle = 45.0;
			this.Distance = 800.0;
			this.IsUseCustomAngle = false;
		}

		protected double formatAngle(double angle)
		{
			int num = (int)(angle / Math.PI*2);
			double num2 = angle - (double)num * Math.PI*2;
			if (num2 < 0.0)
			{
				num2 += Math.PI*2;
			}
			return num2;
		}

		private double FeetToMM(double v)
		{
			return v * 304.8;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			if (!this.m_ptCur.IsEmpty && paintEventArgs != null)
			{
				try
				{
					this.UpdateData();
					double textLength = UnifiedModified.GetTextLength(true, true);
					this.resultLines.Clear();
					XYZ xyz = base.Client2Revit(this.m_ptCur);
					XYZ xyz2 = new XYZ(xyz.X, xyz.Y, this.StartPoint.Z);
					XYZ xyz3 = null;
					XYZ xyz4 = null;
					this.Distance = this.FeetToMM(Math.Abs(xyz2.Y - this.StartPoint.Y));
					this.resultLines = LeaderDimJig.DrawSinglePipeMarkLines(this.StartPoint, xyz2, 45.0, true, this.Distance, textLength, ref xyz3, ref xyz4);
					XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
					foreach (Line curve in this.resultLines)
					{
						new XLine2D(base.Revit2Client(curve.GetEndPoint(0)), base.Revit2Client(curve.GetEndPoint(1))).Draw(g);
					}
				}
				catch (Exception ex)
				{
					ex.ToString();
				}
			}
		}

		public static List<Line> DrawSinglePipeMarkLines(XYZ pipePoint, XYZ location, double angle, bool isCustomAngle, double distance, double textLength, ref XYZ textLocation, ref XYZ endpoint)
		{
			List<Line> list = new List<Line>();
			try
			{
				double num = angle * Math.PI / 180.0;
				XYZ xyz = new XYZ(location.X, pipePoint.Y, pipePoint.Z);
				XYZ xyz2 = new XYZ(pipePoint.X, location.Y, pipePoint.Z);
				XYZ xyz3 = location - xyz2;
				XYZ xyz4 = location - xyz;
				if (isCustomAngle)
				{
					num = angle * Math.PI / 180.0;
				}
				else
				{
					num = Math.Asin(Math.Abs(location.Y - pipePoint.Y) / location.DistanceTo(pipePoint));
				}
				double num2 = AssistFunc.mmToFeet(distance) * Math.Tan(Math.PI*.5 - num);
				endpoint = pipePoint.Add(xyz3.Normalize() * num2).Add(xyz4.Normalize() * AssistFunc.mmToFeet(distance));
				list.Add(YJKLineEx.YJKGetBound(pipePoint, endpoint));
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return list;
		}

		public List<Line> resultLines = new List<Line>();

		public const double _epsDouble = 1E-09;
	}
}
